package com.junzhiit.nengji.app.web.util.aliyun;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bainaeco.framework.util.JsonUtil;
import com.junzhiit.nengji.app.web.exception.BankCardNotMatchingException;
import com.junzhiit.nengji.app.web.exception.BankNotFoundException;
import com.junzhiit.nengji.app.web.exception.CheckFailException;
import com.junzhiit.nengji.app.web.util.aliyun.bean.BankData;
import com.junzhiit.nengji.app.web.util.aliyun.bean.CompanyBankCardData;
import com.junzhiit.nengji.app.web.vo.ResultVO;


public class BankUtil {

	private static Log logger = LogFactory.getLog(BankUtil.class);
	
	private static String APPCODE = "0892caa735af4be3a1018f9ed735faf0";
	
	private static int code= 0;
	
	private static int resultNum = 1;
	
	public static BankData checkBank(String number,String name,String idNumber,String mobilePhone){
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	    Map<String, String> bodys = new HashMap<String, String>();
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("cardNo", number);
	    querys.put("name", name);
	    querys.put("idNo", idNumber);
	    querys.put("phoneNo", mobilePhone);
	    querys.put("ReturnBankInfo", "YES");
	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doPost(Config.URL_BANK_NUMBER_CHECK_HOST,Config.URL_BANK_NUMBER_CHECK_PATH, RequestMethod.POST.toString(), headers,bodys, querys);
	    	String content = EntityUtils.toString(response.getEntity());
	    	BankData bankData = JsonUtil.toObject(content, BankData.class);
	    	
	    	if("0000".equals(bankData.getRespCode())){
	    		return bankData;
	    	}else {
	    		return null;
	    	}
	    } catch (Exception e) {
	    	logger.error(e.getMessage());
	    	throw new BankNotFoundException("没有找到相匹配的银行，请尝试更换银行卡！");
	    }
	}
	
	public static ResultVO checkCompanyBank(String acctName,String bankName,String cardno){
		ResultVO result=new ResultVO();
		String host = "http://verifycorp.market.alicloudapi.com";
	    String path = "/lianzhuo/verifyCorpAccount";
	    String method = "GET";
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("acctName", acctName);
	    querys.put("bankName", bankName);
	    querys.put("cardno", cardno);

	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	String content = EntityUtils.toString(response.getEntity());
	    	CompanyBankCardData companyBankCardData = JsonUtil.toObject(content, CompanyBankCardData.class);
	    	
	    	if(companyBankCardData.getCode() == code){
	    		return result;
	    	}else if(companyBankCardData.getCode() == resultNum){
    			return BankUtil.checkResult(companyBankCardData.getRequestNo().toString());
	    	}else{
	    		throw new BankNotFoundException("没有找到相匹配的银行，请尝试更换银行卡！");
	    	}
	    } catch (Exception e) {
	    	logger.error(e.getMessage());
	    	e.printStackTrace();
	    	throw new CheckFailException("验证失败！请检查参数.");
	    }
	}
	
	public static ResultVO checkResult(String requestNo){
		ResultVO result=new ResultVO();
		String host = "http://queryverif.market.alicloudapi.com";
	    String path = "/lianzhuo/queryVerifyResult";
	    String method = "GET";
	    Map<String, String> headers = new HashMap<String, String>();
	    headers.put("Authorization", "APPCODE " + APPCODE);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("requestNo", requestNo);

	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
	    	String content = EntityUtils.toString(response.getEntity());
	    	CompanyBankCardData companyBankCardData = JsonUtil.toObject(content, CompanyBankCardData.class);
	    	
	    	if(companyBankCardData.getCode() == code){
	    		return result;
	    	}else if( companyBankCardData.getCode() == resultNum){
	    		companyBankCardData.setRequestNo(Long.valueOf(requestNo));
	    		result.setData(companyBankCardData);
	    		return result;
	    	}else {
	    		throw new BankCardNotMatchingException("收款银行不存在此银行账号，请核对收款银行与银行账号是否填写正确");
	    	}
	    } catch (Exception e) {
	    	e.printStackTrace();
	    	throw new CheckFailException("验证失败！请检查参数.");
	    }
	}
}
